package vectortile;

import junit.framework.TestCase;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author jlj
 * @Date 2020/12/28 16:50
 * @Description
 * @Modified
 */
public class tilemain extends TestCase {

    private GeometryFactory gf = new GeometryFactory();

    public void testEncode() {
        VectorTileEncoder vtm = new VectorTileEncoder(256);

        List<Coordinate> cs = new ArrayList<Coordinate>();
        cs.add(new Coordinate(3, 6));
        cs.add(new Coordinate(8, 12));
        cs.add(new Coordinate(20, 34));
        Geometry geometry = gf.createLineString(cs.toArray(new Coordinate[cs.size()]));

        cs.add(new Coordinate(33, 72));
        Geometry geometry2 = gf.createLineString(cs.toArray(new Coordinate[cs.size()]));

        Map<String, Object> attributes = new HashMap<String, Object>();

        vtm.addFeature("DEPCNT", attributes, geometry);
        vtm.addFeature("DEPCNT", attributes, geometry2);

        byte[] encoded = vtm.encode();
        assertNotSame(0, encoded.length);

    }

    public void testNullAttributeValue() throws IOException {
        VectorTileEncoder vtm = new VectorTileEncoder(256);
        Geometry geometry = gf.createPoint(new Coordinate(3, 6));

        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put("key1", "value1");
        attributes.put("key2", null);
        attributes.put("key3", "value3");

        vtm.addFeature("DEPCNT", attributes, geometry);

        byte[] encoded = vtm.encode();

//        assertNotSame(0, encoded.length);

        VectorTileDecoder decoder = new VectorTileDecoder();
        assertEquals(1, decoder.decode(encoded, "DEPCNT").asList().size());
        Map<String, Object> decodedAttributes = decoder.decode(encoded, "DEPCNT").asList().get(0).getAttributes();
        assertEquals("value1", decodedAttributes.get("key1"));//这种方式下，如果不相等的话会报错！且不会再执行后面的语句
        assertEquals("value3", decodedAttributes.get("key3"));
        assertFalse(decodedAttributes.containsKey("key2"));

        String pathResult="E:\\gislogs";
        String fileName="test1.mbtiles";
        getFileByBytes(encoded,pathResult,fileName);
        System.out.println("执行完毕");
    }


    public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + "\\" + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
